package cn.pconline.officialclub.web.admin;

import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.officialclub.entity.OClubTag;
import cn.pconline.officialclub.service.OClubTagService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@RequestMapping(value="/admin/brand/tag")
@Controller
public class AdminTagController extends AdminControllerKit{

    @Autowired
    private OClubTagService oClubTagService;
    
    /**
     * 创建接口
     *
     * @param request
     * @param response 
     */
    @RequestMapping(value="/create.do")
    public void createClubTag(HttpServletRequest request,HttpServletResponse response){
        long tagId = WebUtils.paramLong(request, "tagId", 0);
        String tagName = WebUtils.param(request, "tagName", "");
        if(tagId <= 0){
            errorMsg("请输入正确的标签ID", response);
            return;
        }
        if(StringUtils.isBlank(tagName)){
            errorMsg("名称不能为空", response);
            return;
        }
        if(oClubTagService.isExistTagId(tagId)){
            errorMsg("该标签ID已经存在", response);
            return;
        }
        OClubTag oClubTag = new OClubTag();
        oClubTag.setTagId(tagId);
        oClubTag.setTagName(tagName);
        long id = oClubTagService.create(oClubTag);
        if(id>0){
            successMsg(response, "tag-list", "创建成功", "closeCurrent");
        }else{
            errorMsg("创建失败", response);
        }
    }

    /**
     * 更新接口
     *
     * @param request
     * @param response 
     */
    @RequestMapping(value="/update.do")
    public void updateClubTag(HttpServletRequest request,HttpServletResponse response){
        long tagId = WebUtils.paramLong(request, "tagId", 0);
        String tagName = WebUtils.param(request, "tagName", "");
        if(tagId <= 0 ){
            errorMsg("ID有误", response);
            return;
        }
            OClubTag oClubTag = oClubTagService.findById(OClubTag.class, tagId);
            if(oClubTag == null){
                errorMsg("找不到该标签", response);
                return;
            }
            oClubTag.setTagName(tagName);
            int count = oClubTagService.update(oClubTag);
            if (count>0){
                successMsg(response, "tag-list", "更新成功", "closeCurrent");
            }else{
                errorMsg("更新失败", response);
            }
    }

    /**
     * 删除标签
     *
     * @param request
     * @param response 
     */
    @RequestMapping(value="/delete.do",method= RequestMethod.POST)
    public void delete(HttpServletRequest request,HttpServletResponse response){
        long tagId = WebUtils.paramLong(request, "tagId", 0);
        if(tagId<=0){
            errorMsg("该标签不存在", response);
            return;
        }
        OClubTag oClubTag = oClubTagService.findById(OClubTag.class, tagId);
        if(oClubTag == null){
            errorMsg("该标签不存在", response);
            return;
        }
        int count = oClubTagService.delete(oClubTag);
        if(count>0){
            successMsg(response, "tag-list", "删除成功", "reload");
        }else{
            errorMsg("删除失败", response);
        }
    }

    /**
     * 标签列表
     *
     * @param request
     * @param response
     * @return 
     */
    @RequestMapping(value="/list.do")
    public String listTag(HttpServletRequest request,HttpServletResponse response){
        int pageNo = WebUtils.paramInt(request, "pageNo", DEF_PAGE_NO);
        int pageSize = WebUtils.paramInt(request, "pageSize", DEF_PAGE_SIZE);
        Pager<OClubTag> tags = oClubTagService.pagerQuery(pageNo, pageSize);
        request.setAttribute("pager", tags);
        return "admin/official/tag/list";
    }
    
    @RequestMapping(value="/createTag.do")
    public String createTag(HttpServletRequest request,HttpServletResponse resp){
        long tagId = WebUtils.paramLong(request, "tagId", 0);
        if(tagId>0){
            OClubTag o = oClubTagService.findById(OClubTag.class, tagId);
            request.setAttribute("o", o);
        }
        return "admin/official/tag/create";
    }
}
